package org.ithink.util;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;



public class ExcelUtil {
	/**
	 * 
	 * @param excelPath  excel文件路径
	 * @return  行数&列数
	 */
	public  static String returnRow(String excelPath){
		int rowCount = 0;
		int columnCount = 0;
		try {
			if(NullsUtil.isEmpty(excelPath)|| excelPath.trim().equalsIgnoreCase("null")){
				rowCount = 0;
				columnCount = 0;
			}
			else{
				Workbook workbook =null;
				workbook = Workbook.getWorkbook(new File(excelPath));  
				Sheet sheet = workbook.getSheet(0);
				
				rowCount = sheet.getRows();
				columnCount = sheet.getColumns();
			}
		} catch (Exception e) {
			rowCount = 0;
			columnCount = 0;
		}
		
		return rowCount+"&"+columnCount;
	}
	
	
	public static List readFile(String excelPath, String strIndexRow, String strEndRow, String strIndexColumn, String strEndColumn, String errorMsg) throws Exception{
		Workbook workbook =null;
		int indexRowCount = 0;
		int endRowCount = 0;
		int indexColumn = 0;
		int endColumn = 0;
		try { 
			workbook = Workbook.getWorkbook(new File(excelPath));  
			
			Sheet sheet = workbook.getSheet(0);
			Cell cell=null;
			
			//判断行
			if(strIndexRow==null ||strIndexRow.trim()==""){
				indexRowCount = 0;
			}
			else{
				indexRowCount = Integer.parseInt(strIndexRow);
			}
			if(strEndRow==null ||strEndRow.trim()==""){
				endRowCount = sheet.getRows(); 
			}
			else{
				endRowCount = Integer.parseInt(strEndRow);
			}
			
			//判断列
			if(strIndexColumn==null ||strIndexColumn.trim()==""){
				indexColumn = 0;
			}
			else{
				indexColumn = Integer.parseInt(strIndexColumn)-1;
			}
			if(strEndColumn==null ||strEndColumn.trim()==""){
				endColumn = sheet.getRows(); 
			}
			else{
				endColumn = Integer.parseInt(strEndColumn);
			}
			
			List oneRowList = new ArrayList();    //  定义一个list存放一行的数据
			List sRowList = new ArrayList();      //  存放oneRowList
			
			
			for (int i = indexRowCount; i <endRowCount; i++) { 
				//遍历每行前先初始化list
				oneRowList = new ArrayList();
				for(int j = indexColumn; j < endColumn; j++){
					cell = sheet.getCell(j,i);
					oneRowList.add(cell.getContents());
				}  //内层for end
				sRowList.add(oneRowList);
			}  //外层for  end
			return sRowList;
		} catch (Exception e) { 
			//return new ArrayList();
			throw new RuntimeException(errorMsg);   
		} 
		
    }
	
	
	public static List readFileAll(String excelPath) throws Exception{
		Workbook workbook =null;
		Map oneRowMap = new LinkedHashMap();    //  定义一个map存放一行的数据
		List sRowList = new ArrayList();      //  存放oneRowList
		try { 
			workbook = Workbook.getWorkbook(new File(excelPath));  
			
			Sheet sheet = workbook.getSheet(0);
			
			int endRowCount = sheet.getRows();
			int endColumn = sheet.getColumns();
			
			for (int i = 1; i <endRowCount; i++) { 
				//遍历每行前先初始化map
				oneRowMap = new LinkedHashMap();
				for(int j = 0; j < endColumn; j++){
					String title = sheet.getCell(j, 0).getContents();
					String value = sheet.getCell(j,i).getContents();
					oneRowMap.put(title, value);
				}  //内层for end
				sRowList.add(oneRowMap);
			}  //外层for  end
			//return sRowList;
			/*for(int i=0; i<sRowList.size(); i++){
				Map oneMap = (Map) sRowList.get(i);
				Iterator it = oneMap.entrySet().iterator();
		        while (it.hasNext()) {
		            Map.Entry entry = (Map.Entry) it.next();
		            Object key = entry.getKey(); 
		            Object value = entry.getValue(); 
		            System.out.println(key);
		            System.out.println(value);
		        }
		
			}*/
		} catch (Exception e) { 
			sRowList = new ArrayList(); 
		} 
		//System.out.println("工具类里读出来的行数:"+sRowList.size());
		return sRowList;
		
    }
	
	
	public static void main(String[] args){
		
	}

}
